package com.yqg.manage.controller;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.entity.Archive;
import com.yqg.manage.entity.ArchiveBranchNode;
import com.yqg.manage.entity.Case;
import com.yqg.manage.mapper.ArchiveNodeMapper;
import com.yqg.manage.mapper.CaseMapper;
import com.yqg.manage.service.IArchiveBranchNodeService;
import com.yqg.manage.service.IArchiveService;
import com.yqg.manage.service.ICaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IArchiveNodeService;
import com.yqg.manage.entity.ArchiveNode;

import org.springframework.web.bind.annotation.RestController;

/**
 * ArchiveNodeController 前端控制器
 *
 * @author zxf
 * @since 2024-04-30
 */
@RestController
@RequestMapping("/archive-node")
public class ArchiveNodeController {

    @Resource
    private IArchiveNodeService archiveNodeService;

    @Resource
    private IArchiveService archiveService;

    @Resource
    private ICaseService caseService;

    @Resource
    private CaseMapper caseMapper;

    @Autowired
    ArchiveNodeMapper archiveNodeMapper;

    @Autowired
    IArchiveBranchNodeService archiveBranchNodeService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody ArchiveNode archiveNode) {
        return Result.success(archiveNodeService.saveOrUpdate(archiveNode));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        ArchiveNode archiveNode = archiveNodeService.getById(id);
        return Result.success(archiveNodeService.saveOrUpdate(archiveNode));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<ArchiveNode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<ArchiveNode> list = archiveNodeService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(archiveNodeService.getById(id));
    }


    /**
     * 获取案件当前可添加文书
     */
    @GetMapping("/getNowDocument")
    public Result getNowDocument(@RequestParam Integer routineTypeId, @RequestParam Integer archiveNodeId) {
        Map<String, String> map = new HashMap<>();
        for (Archive archive : archiveNodeMapper.getNowDocument(routineTypeId, archiveNodeId)) {
            map.put(archive.getTemplateNo(), archive.getDocumentName());
        }
        return Result.success(map);
    }


    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<ArchiveNode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(archiveNodeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


    @GetMapping("/getNextDocumentType")
    public Result findNextArchives(@RequestParam Long id,
                                   @RequestParam Long caseId) {
        ArchiveBranchNode archiveBranchNode = archiveBranchNodeService.getById(id);
        LambdaQueryWrapper<ArchiveNode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArchiveNode::getNodeId,archiveBranchNode.getNextNodeId())
                .eq(ArchiveNode::getIsDelete,"0");
        List<ArchiveNode> archiveNodes = archiveNodeService.list(queryWrapper);

        // 修改案件的阶段
        Case acase = caseMapper.getCaseById(caseId);
        if (!Objects.equals(acase.getRoutineTypeId(), archiveNodes.get(0).getRoutineTypeId())) {
            acase.setRoutineTypeId(archiveNodes.get(0).getRoutineTypeId());
        }
        acase.setArchiveNodeId(archiveNodes.get(0).getNodeId());

        List<Long> archiveBranchNodeIds = acase.getArchiveBranchNodeIds();
        System.out.println(JSON.toJSONString(acase));
        if (id == 26 || id == 25) {
            if (id == 26 && !archiveBranchNodeIds.contains(26L)) {
                archiveBranchNodeIds.add(id);
            } else if (id == 25 && !archiveBranchNodeIds.contains(25L)) {
                archiveBranchNodeIds.add(id);
            }
        } else {
            archiveBranchNodeIds.add(id);
        }
        acase.setArchiveBranchNodeIds(archiveBranchNodeIds);
        caseService.saveOrUpdate(acase);

        List<Long> archiveIds = archiveNodes.stream().map(ArchiveNode::getArchiveId).collect(Collectors.toList());
        LambdaQueryWrapper<Archive> archiveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        archiveLambdaQueryWrapper.in(Archive::getId, archiveIds);
        Map<String, String> map = new HashMap<>();
        for (Archive archive : archiveService.list(archiveLambdaQueryWrapper)) {
            map.put(archive.getTemplateNo(), archive.getDocumentName());
        }
        return Result.success(map);
    }

}

